package com.jinlong.common.log;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LogAspectServiceApi {
    private JSONObject jsonObject = new JSONObject();


    //申请一个切入点
    @Pointcut("execution(public * com.jinlong.api.server.*.*(..))")
    private void controllerAspect(){

    }
    @Before(value = "controllerAspect()")
    public void methodBefore(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        log.info("======================请求内容==================");
        try{
            //打印内容
            log.info("请求地址："+request.getRequestURL().toString());
            log.info("请求方式："+request.getMethod());
            log.info("请求类方法："+joinPoint.getSignature());
            log.info("请求类方法参数"+ Arrays.toString(joinPoint.getArgs()));

        }catch (Exception e){

            log.error("########LogAspectServiceApi.class methodBefore() ###ERROR:"+e);
        }
        log.info("============请求内容============");
    }
    @AfterReturning(returning="o",pointcut = "controllerAspect()")
    public void methodAfterReturing(Object o){
        log.info("-------------返回的内容--------------");
        try{
            log.info("Response内容："+jsonObject.toJSONString(o));
        }catch (Exception e){
            log.error("########LogAspectServiceApi.class methodAfterReturing() ###ERROR:"+e);
        }
    }
}
